package hrms.util.amq;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer {

	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;

	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;

	private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

	ConnectionFactory connectionFactory;

	Connection connection;

	Session session;

	public String mes;

	public String error;
	
	public final List<String> list = new ArrayList<String>();

	static ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<MessageConsumer>();

	private static Consumer tComsumer = null;

	private Consumer() {
		init();
	}

	public static synchronized Consumer getInstance() {
		if (tComsumer == null) {
			tComsumer = new Consumer();
		}
		return tComsumer;
	}

	public void init() {     
		try {
			connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
			connection = (Connection) connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public boolean getMessage(String disname) {
		try {
			Queue queue = session.createQueue(disname);
			MessageConsumer consumer = null;

			if (threadLocal.get() != null) {
				consumer = threadLocal.get();
			} else {
				consumer = session.createConsumer(queue);
				threadLocal.set(consumer);
			}
//			receive()方法接收			
			TextMessage msg = (TextMessage) consumer.receiveNoWait();		
			if (msg != null) {
				mes = msg.getText();
				msg.acknowledge();
				return true;
			} else {
				error = "AMQ中无可用消息";
				return false;
			}
		} catch (JMSException e) {
			e.printStackTrace();
			error = "JMSException 异常终止";
			return false;
		}
	}
	
	public boolean getMessageListener(String disname) {
		try {
			Queue queue = session.createQueue(disname);
			MessageConsumer consumer = null;

			if (threadLocal.get() != null) {
				consumer = threadLocal.get();
			} else {
				consumer = session.createConsumer((Destination) queue);
				threadLocal.set(consumer);
			}
			consumer.setMessageListener(new MessageListener() {
			    public void onMessage(Message message) {
			        try {
			            mes = ((TextMessage) message).getText();
			            ((TextMessage) message).acknowledge();
			            list.add(mes);
			        } catch (JMSException e) {
			            e.printStackTrace();
			        }
			    }

			});
			return true;
		} catch (JMSException e) {
			e.printStackTrace();
			error = "JMSException 异常终止";
			return false;
		}
	}
	
	
}